package com.xjscrm.console.chat.core.impl.conversation;

import cn.hutool.core.util.IdUtil;
import com.uzai.mobile.collect.api.MicaTopics;
import com.uzai.mobile.collect.api.dto.MicaTriggerDto;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerCustomerSetRemarkMsg;
import com.uzai.mobile.collect.api.dto.trigger.MicaTriggerMsg;
import com.uzai.mobile.collect.api.dto.trigger.group.MicaTriggerGroupSetNameMsg;
import com.uzai.mobile.collect.api.enums.MicaTriggerType;
import com.xjscrm.common.enums.ChatConversationTypeEnum;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.console.chat.core.ChatBaseMsgHandler;
import com.xjscrm.console.chat.enums.ChatMsgType;
import com.xjscrm.console.chat.frame.recv.RConversationSetRemarkMsg;
import com.xjscrm.console.chat.frame.send.SResultMsg;
import com.xjscrm.console.entity.ChatTalkSession;
import com.xjscrm.console.mq.producer.SendMsgProducer;
import com.xjscrm.console.service.customer.chat.ChatTalkSessionService;
import com.xjscrm.console.service.customer.wwDevice.WwDeviceService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author mingyang.chen create in 2024/3/17 09:42
 */
@Component
@Slf4j
public class ChatConversationSetRemarkMsgHandler extends ChatBaseMsgHandler<RConversationSetRemarkMsg> {

    @Autowired
    private SendMsgProducer sendMsgProducer;

    @Autowired
    private WwDeviceService wwDeviceService;

    @Autowired
    private ChatTalkSessionService chatTalkSessionService;

    @Override
    public Class<RConversationSetRemarkMsg> msgClass() {
        return RConversationSetRemarkMsg.class;
    }

    @Override
    public boolean checkMsg(RConversationSetRemarkMsg baseMsg) {
        return super.checkMsg(baseMsg) || !ObjectUtils.isNotEmpty(baseMsg.getConversationName()) || !Objects.nonNull(baseMsg.getTalkSessionId());
    }

    @Override
    public SResultMsg doHandle(RConversationSetRemarkMsg content, ChannelHandlerContext ctx) {

        ChatTalkSession chatTalkSession = chatTalkSessionService.getById(content.getMerId(), content.getTalkSessionId());
        if (Objects.isNull(chatTalkSession)) {
            throw new BusinessException("会话不存在, 无法修改会话名称");
        }

        ChatTalkSession update = new ChatTalkSession();
        update.setId(chatTalkSession.getId());
        update.setMerId(content.getMerId());
        update.setConversationName(content.getConversationName());
        update.setUpdateTime((int) (System.currentTimeMillis() / 1000L));

        chatTalkSessionService.update(update);

        MicaTriggerMsg triggerMsg;
        MicaTriggerType triggerType;
        if (chatTalkSession.getType().equals(ChatConversationTypeEnum.prv_conversation.getCode())) {
            MicaTriggerCustomerSetRemarkMsg customerSetRemarkMsg = new MicaTriggerCustomerSetRemarkMsg();
            customerSetRemarkMsg.setRemark(content.getConversationName());
            customerSetRemarkMsg.setWxid(chatTalkSession.getConversationId());
            triggerMsg = customerSetRemarkMsg;
            triggerType = MicaTriggerType.TRIGGER_SET_REMARK;
        }
        //
        else {
            MicaTriggerGroupSetNameMsg micaTriggerGroupSetNameMsg = new MicaTriggerGroupSetNameMsg();
            micaTriggerGroupSetNameMsg.setGroupId(chatTalkSession.getConversationId());
            micaTriggerGroupSetNameMsg.setNewGroupName(content.getConversationName());

            triggerMsg = micaTriggerGroupSetNameMsg;
            triggerType = MicaTriggerType.TRIGGER_GROUP_SET_NAME;
        }

        MicaTriggerDto micaTriggerDto = MicaTriggerDto.newInstance(triggerMsg, triggerType, item -> {
            item.setReqId(IdUtil.getSnowflakeNextId());
            item.setDeviceId(chatTalkSession.getDeviceId());
            item.setDeviceType(chatTalkSession.getDeviceType());
        });

        sendMsgProducer.pushMsg(micaTriggerDto, MicaTopics.WW_TRIGGER_TOPIC);

        return SResultMsg.success(content, "会话更名成功");
    }

    @Override
    public ChatMsgType msgType() {
        return ChatMsgType.r_conversation_set_remark;
    }
}
